package radio;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class Radio {

	private HashMap<String, Cliente> ClientesDaRadio = new HashMap<>();
	private HashMap<String, Musica> MusicasDaRadio = new HashMap<>();
	private ArrayList<Avaliacao> AvaliacoesDaRadio = new ArrayList<>();

	@Override
	public String toString() {
		return "Radio [clientesDaRadio=" + ClientesDaRadio
				+ ", musicasDaRadio=" + MusicasDaRadio + ", avaliacoesDaRadio="
				+ AvaliacoesDaRadio + "]";
	}

	public void adicionarCliente(Cliente ClienteNovo) {

		this.ClientesDaRadio.put(ClienteNovo.getNome(), ClienteNovo);
	}

	public void adicionarMusica(Musica MusicaNova) {

		this.MusicasDaRadio.put(MusicaNova.getNomeDaMusica(), MusicaNova);
	}

	public void adicionarAvaliacao(Avaliacao AvaliacaoNova) {

		this.AvaliacoesDaRadio.add(AvaliacaoNova);
	}

	public Cliente getCliente(Cliente ClienteProcurado) {

		return this.ClientesDaRadio.get(ClienteProcurado.getNome());
	}

	public Cliente getCliente(String ClienteProcurado) {

		return this.ClientesDaRadio.get(ClienteProcurado);
	}

	public Musica getMusica(Musica MusicaProcurada) {

		return this.MusicasDaRadio.get(MusicaProcurada.getNomeDaMusica());
	}

	public Musica getMusica(String MusicaProcurada) {

		return this.MusicasDaRadio.get(MusicaProcurada);
	}

	public Avaliacao getAvaliacao(Avaliacao AvaliacaoProcurada) {

		for (Avaliacao avaliacao : this.AvaliacoesDaRadio) {
			if (avaliacao == AvaliacaoProcurada) {
				return avaliacao;
			}
		}
		return null;
	}

	public void carregarClientes() {
		Path path = Paths
				.get("./Arquivos/Clientes.txt");
		Charset utf8 = StandardCharsets.UTF_8;
		try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
			String line = null;
			String[] stringsSeparadas;
			while ((line = reader.readLine()) != null) {
				stringsSeparadas = line.split(";");
				Cliente clienteNovo = new Cliente(stringsSeparadas[0].trim(),
						stringsSeparadas[1].trim());
				this.adicionarCliente(clienteNovo);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void carregarMusicas() {
		Path path = Paths
				.get("./Arquivos/Musicas.txt");
		Charset utf8 = StandardCharsets.UTF_8;
		try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
			String line = null;
			String[] stringsSeparadas;
			while ((line = reader.readLine()) != null) {
				stringsSeparadas = line.split(";");
				Musica MusicaNova = new Musica(stringsSeparadas[0].trim(),
						stringsSeparadas[1].trim(),
						Integer.parseInt(stringsSeparadas[2].trim()),
						Integer.parseInt(stringsSeparadas[3].trim()),
						Integer.parseInt(stringsSeparadas[4].trim()));
				this.adicionarMusica(MusicaNova);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void carregarAvaliacoes() {
		Path path = Paths
				.get("C:/Users/WESLEY/workspace/Radio/Arquivos/Avaliacoes.txt");
		Charset utf8 = StandardCharsets.UTF_8;
		try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
			String line = null;
			String[] stringsSeparadas;
			while ((line = reader.readLine()) != null) {
				stringsSeparadas = line.split(";");
				Avaliacao AvaliacaoNova = new Avaliacao(
						this.getCliente(stringsSeparadas[0].trim()),
						this.getMusica(stringsSeparadas[1].trim()),
						Integer.parseInt(stringsSeparadas[2].trim()));
				this.adicionarAvaliacao(AvaliacaoNova);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<String> imprimirListaDeClientes() {
		ArrayList<String> clientes = new ArrayList<>();
		Set<String> keys = this.ClientesDaRadio.keySet();
		for (String string : keys) {
			clientes.add(this.ClientesDaRadio.get(string).toString() + "\n");
			System.out.println(this.ClientesDaRadio.get(string));
		}
		return clientes;
	}

	public void imprimirListaDeMusicas() {

		Set<String> keys = this.MusicasDaRadio.keySet();
		for (String string : keys) {
			System.out.println(this.MusicasDaRadio.get(string));
		}
	}

	public void imprimirListaDeAvaliacoes() {

		for (Avaliacao avaliacao : this.AvaliacoesDaRadio) {
			System.out.println(avaliacao);
		}

	}
}
